package com.caishi.lkx.exam.ienum.type;

import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.caishi.lkx.exam.ienum.BaseEnum;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;

import java.io.IOException;
import java.lang.reflect.Type;

/**
 * @author by Jansia
 * @Date 2022/1/7 15:20
 * @Description
 */
public enum QuestionType implements BaseEnum<Integer> {
    // 试题题型枚举
    singleChoice("单选题", 1),
    multipleChoice("多选题", 2),
    judgment("判断题", 3),
    fill("填空题", 4),
    text("简答题", 5),
    combination("组合题", 6),
    indefiniteMultipleChoice("不定项选择题", 7)
    ;

    @Getter
    final
    String desc;

    @Getter
    @EnumValue
    @JsonValue
    @JSONField(serialize = true)
    final
    Integer code;

    QuestionType(String desc, Integer code) {
        this.desc = desc;
        this.code = code;
    }
    @Override
    public Integer getCode() {
        return code;
    }

    @JsonCreator
    public static QuestionType fromCode(Integer code) {
        return BaseEnum.fromCode(QuestionType.class, code);
    }
    public static class QuestionTypeSerializer implements ObjectSerializer {

        @Override
        public void write(JSONSerializer jsonSerializer, Object o, Object o1, Type type, int i) throws IOException {
            QuestionType questionType = (QuestionType) o;
            // 只输出 code
            SerializeWriter out = jsonSerializer.getWriter();
            out.writeInt(questionType.getCode());
        }
    }

    // 将自定义序列化器应用到枚举类的字段
    @JSONField(serializeUsing = QuestionTypeSerializer.class)
    public Integer getCodeForSerialization() {
        return this.code;
    }


}
